﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace WebSite.FengYi.Forms
{
    public partial class FormEdit : Form
    {
        private int _searchIndex = 0;//查找开始位置
        public string editfile;
        public FormEdit(string file)
        {
            this.editfile = file;
           
            InitializeComponent();
        }

        private void btnViewPic_Click(object sender, EventArgs e)
        {
            var findWord = tbSearch.Text.Trim();
            if (string.IsNullOrEmpty(findWord) || string.IsNullOrEmpty(tbEditor.Text.Trim())) return;

            var searchResult = tbEditor.Find(findWord, _searchIndex, RichTextBoxFinds.None);
            if (searchResult == -1)
            {
                if (_searchIndex == 0)
                {
                    /* MessageBoxTimeOut
                     * 自定义的延时自动关闭提示，可以用 MessageBox提示用户
                     */
                    MessageBox.Show(@"未找到相关内容！", @"提示");
                }

                /* _searchIndex>0 
                 * 说明之前有查找结果，并且已经越过最后一个查找结果位置后，则自动从头开始查找。
                 * 也可以提示用户是否从头查找。
                 */
                _searchIndex = 0;
            }
            else
            {
                tbEditor.Focus();
                tbEditor.Select(searchResult, findWord.Length);
                _searchIndex = searchResult + findWord.Length;
            }
        }

        private void FormEdit_Load(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(this.editfile, System.Text.Encoding.UTF8);
            this.Text = this.Text + ":" + this.editfile;
            tbEditor.Text = sr.ReadToEnd();
            sr.Close();


            //tbEditor.LoadFile(this.editfile, System.Windows.Forms.RichTextBoxStreamType.PlainText);
        }
        private void getCmd()
        {
            
            //String nginx = Application.StartupPath + "\\start_nginx.bat";
       
           



        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            StreamWriter sw = new StreamWriter(this.editfile, false, System.Text.Encoding.UTF8);// Encoding.GetEncoding("UTF-8"));

            sw.Write(tbEditor.Text);//.Trim());

            sw.Close();
            //StreamWriter sw = new StreamWriter((this.editfile, System.Text.Encoding.UTF8);
            //tbEditor.SaveFile(this.editfile, RichTextBoxStreamType.PlainText);
        }
    }
}
